'''
Created on Oct 15, 2012

@author: raygomez
'''
import unittest
from MessageHeader import MessageHeader
from xdrlib import Packer, Unpacker
import binascii


class Test(unittest.TestCase):


    def testMessageHeader(self):
        header = MessageHeader()
        header.setRequest(False)
        header.setProxiable(False)
        header.setError(False)
        header.setRetransmit(False)
        
        self.assertFalse(header.isRequest())
        self.assertFalse(header.isProxiable())
        self.assertFalse(header.isError())
        self.assertFalse(header.isRetransmit())
        
        header1 = MessageHeader()
        header1.setRequest(True)
        header1.setProxiable(True)
        header1.setError(True)
        header1.setRetransmit(True)

        header2 = MessageHeader(header1)
        self.assertTrue(header2.isRequest())
        self.assertTrue(header2.isProxiable())
        self.assertTrue(header2.isError())
        self.assertTrue(header2.isRetransmit())

    def testEncodeSize(self):
        header = MessageHeader()
        self.assertEqual(20, header.encodeSize())

    def testEncodeDecode(self):
        packer = Packer()
        header = MessageHeader()
        header.version = 0x01
        header.commandFlags = 0x40
        header.commandCode = 0x1234
        header.applicationID = 0x12345678
        header.hopByHopIdentifier = 1
        header.endToEndIdentifier = 2
        header.encode(packer, 12)
        self.assertEqual(20, header.encodeSize())
        unpacker = Unpacker(packer.get_buffer())

        header2 = MessageHeader()
        header2.decode(unpacker)
        self.assertEqual(0x01, header2.version)
        self.assertEqual(0x40, header2.commandFlags)
        self.assertEqual(0x1234, header2.commandCode)
        self.assertEqual(0x12345678, header2.applicationID)
        self.assertEqual(1, header2.hopByHopIdentifier)
        self.assertEqual(2, header2.endToEndIdentifier)
        
    def testPrepareResponse(self):
        header = MessageHeader()
        header.version = 0x01
        header.commandFlags = 0x80
        header.commandCode = 0x1234
        header.applicationID = 0x12345678
        header.hopByHopIdentifier = 1
        header.endToEndIdentifier = 2
        
        header2 = MessageHeader()
        header2.prepareResponse(header)
        self.assertEqual(0x01, header2.version)
        self.assertEqual(0x00, header2.commandFlags)
        self.assertEqual(0x1234, header2.commandCode)
        self.assertEqual(0x12345678, header2.applicationID)
        self.assertEqual(1, header2.hopByHopIdentifier)
        self.assertEqual(2, header2.endToEndIdentifier)
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()